/*****************************************************
*                                                                          
*               COPYRIGHT (c) 2015-2017 Broadlink Corporation              
*                         All Rights Reserved                              
*                                                                          
* The source code contained or described herein and all documents          
* related to the source code ("Material") are owned by Broadlink         
* Corporation or its licensors.  Title to the Material remains             
* with Broadlink Corporation or its suppliers and licensors.               
*                                                                          
* The Material is protected by worldwide copyright and trade secret        
* laws and treaty provisions. No part of the Material may be used,         
* copied, reproduced, modified, published, uploaded, posted, transmitted,  
* distributed, or disclosed in any way except in accordance with the       
* applicable license agreement.                                            
*                                                                          
* No license under any patent, copyright, trade secret or other            
* intellectual property right is granted to or conferred upon you by       
* disclosure or delivery of the Materials, either expressly, by            
* implication, inducement, estoppel, except in accordance with the         
* applicable license agreement.                                            
*                                                                          
* Unless otherwise agreed by Broadlink in writing, you may not remove or   
* alter this notice or any other notice embedded in Materials by Broadlink 
* or Broadlink's suppliers or licensors in any way.                        
*                                                                          
** Author: shengda.huang
** Date: 2019-02-18
*
*****************************************************/

#ifndef __DYMEM_H_
#define __DYMEM_H_

#ifdef __cplusplus
extern "C"{
#endif

/***** INCLUDES *************************************/
#include "common.h"

/***** DEFINES **************************************/
#define HEAP_MAGIC 0x1ea0

/***** TYPEDEFS *************************************/
struct SHeapMem 
{
    /* magic and used flag */
    u16 magic;
    u16 used;

    u32 next, prev;
};

typedef struct 
{
#ifdef DYMEM_USING_HOOK
	void (*dymem_malloc_hook)(void *ptr, u32 size);
	void (*dymem_free_hook)(void *ptr);
#endif
	/** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
	u8 *heap_ptr;
	/** the last entry, always unused! */
	struct SHeapMem *heap_end;
	/* pointer to the lowest free block */
	struct SHeapMem *lfree;   

	u32 mem_size_aligned;

#ifdef DYMEM_DEBUG_ENABLE
	u32 used_mem, max_mem;
#endif
} dymem_obj_t;

/***** CONSTANTS ************************************/

/***** VARIABLES ************************************/

/***** EXTERNAL VARIABLES ***************************/

/***** EXTERNAL FUNCTIONS ***************************/

/***** PROTOTYPES ***********************************/
void dymem_heap_init(dymem_obj_t *_this, void *begin_addr, void *end_addr);
void *dymem_malloc(dymem_obj_t *_this, u32 size);
void *dymem_realloc(dymem_obj_t *_this, void *rmem, u32 newsize);
void *dymem_calloc(dymem_obj_t *_this, u32 count, u32 size);
void dymem_free(dymem_obj_t *_this, void *rmem);

void dymem_info(dymem_obj_t *_this, u32 *total, u32 *used, u32 *max_used);
void dymem_list(dymem_obj_t *_this);


#ifdef __cplusplus
}
#endif

#endif /* __DYMEM_H_ */

